import os
from sys import argv


def errorPrint(str):
    print('\u001b[31m%s\u001b[0m'%str)


def successPrint(str):
    print('\u001b[33m%s\u001b[0m' % str)


def printTitle(title):
    print('\u001b[35;1m======================%s=====================\u001b[0m'%title)


def list_file(dir_path='.', suffix='.py'):
    return [x for x in os.listdir(dir_path) if os.path.splitext(x)[1] == suffix]


def main():
    if(len(argv)<2):
        errorPrint("你必须指定文件夹地址")
        return
    wpath = argv[1]
    neededPyFile = ['commens.py', 'MAPPING.py', 'OUTPARAM.py', 'PARAM.py', 'COMPARE.py', 'REFORM.py', 'ALIGN.py']
    pyFile = list_file()
    print('找到了python文件：')
    for file in pyFile:
        print(file)
    if set(neededPyFile).issubset(set(pyFile)):
        print('所需文件齐全')
        jsonFile = list_file(suffix='.json')
        if 'REFORM.json' in jsonFile:
            os.system('python ALIGN.py %s'% wpath)
            return
        if len(argv) > 2:
            startStage = f'{argv[2]}.py'
        else:
            startStage = neededPyFile[1]
        if startStage in neededPyFile:
            yes = False
            for file in neededPyFile:
                if startStage == file:
                    yes = True
                if yes:
                    printTitle(file)
                    if file == "MAPPING.py" or file == "ALIGN.py":
                        yes = not os.system('python ./%s %s' % (file,wpath))
                    else:
                        yes = not os.system('python ./%s' % file)
            if yes:
                successPrint("Done,所有阶段已经全部完成")
            else:
                errorPrint('ERROR:脚本没有全部执行完成')
        else:
            errorPrint('ERROR:您输入的参数有误,请输入正确的阶段名')
    else:
        errorPrint('ERROR:所需文件缺失，无法执行')


if __name__ == '__main__':
    main()

